package com.yzt.message.custom;

import android.os.Parcel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yzt.zhuju.rongyun.IMMessageType;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.List;

import io.rong.common.ParcelUtils;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

/**
 * 创建群聊
 */
@MessageTag(value = IMMessageType.GROUP_CREATE, flag = MessageTag.ISPERSISTED | MessageTag.ISCOUNTED)
public class RXCreateGroupContent extends MessageContent {

    private String content;
    private String invitedUserId;
    private String invitedUserName;
    private List<String> beInvitedUsers;

    @Override
    public byte[] encode() {
        JSONObject object = new JSONObject();
        object.put("content", content);
        object.put("invitedUserId", invitedUserId);
        object.put("invitedUserName", invitedUserName);
        object.put("beInvitedUsers", beInvitedUsers);
        return object.toString().getBytes(StandardCharsets.UTF_8);

    }

    public RXCreateGroupContent() {

    }

    public RXCreateGroupContent(byte[] data) {
        super(data);
        String jsonStr = null;
        try {
            jsonStr = new String(data, StandardCharsets.UTF_8);
            JSONObject object = JSON.parseObject(jsonStr);
            setContent(object.getString("content"));
            setInvitedUserId(object.getString("extra"));
            setInvitedUserName(object.getString("invitedUserName"));
            setBeInvitedUsers(JSON.parseArray(object.getString("beInvitedUsers"), String.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        ParcelUtils.writeToParcel(dest, content);
        ParcelUtils.writeToParcel(dest, invitedUserId);
        ParcelUtils.writeToParcel(dest, invitedUserName);
        ParcelUtils.writeToParcel(dest, beInvitedUsers);
    }

    public static final Creator<RXCreateGroupContent> CREATOR = new Creator<RXCreateGroupContent>() {

        @Override
        public RXCreateGroupContent createFromParcel(Parcel source) {
            return new RXCreateGroupContent(source);
        }

        @Override
        public RXCreateGroupContent[] newArray(int size) {
            return new RXCreateGroupContent[size];
        }
    };


    public RXCreateGroupContent(Parcel parcel) {
        content = ParcelUtils.readFromParcel(parcel);
        invitedUserId = ParcelUtils.readFromParcel(parcel);
        invitedUserName = ParcelUtils.readFromParcel(parcel);
        beInvitedUsers = ParcelUtils.readListFromParcel(parcel, String.class);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getInvitedUserId() {
        return invitedUserId;
    }

    public void setInvitedUserId(String invitedUserId) {
        this.invitedUserId = invitedUserId;
    }

    public String getInvitedUserName() {
        return invitedUserName;
    }

    public void setInvitedUserName(String invitedUserName) {
        this.invitedUserName = invitedUserName;
    }

    public List<String> getBeInvitedUsers() {
        return beInvitedUsers;
    }

    public void setBeInvitedUsers(List<String> beInvitedUsers) {
        this.beInvitedUsers = beInvitedUsers;
    }
}

